//
//  FLMNavigationController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/6.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMNavigationController.h"

@interface FLMNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation FLMNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    
    //设置手势代理
    pan.delegate = self;
    
    //给view添加手势
    [self.view addGestureRecognizer:pan];

    
}

#pragma mark - UIGestureRecognizerDelegate
// 是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 在根控制器下 不要 触发手势
    return self.childViewControllers.count > 1;
}


- (void)handleNavigationTransition:(UINavigationController *)nav
{
//        NSLog(@"%s-------",__func__);
}

+ (void)load
{
 
    //创建当前类导航条
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    //设置导航条的标题字体
    NSMutableDictionary *attDictM = [NSMutableDictionary dictionary];
    //设置key值得的字体大小
    attDictM[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    
    navBar.titleTextAttributes = attDictM;
    //设置导航条的背景色
    [navBar xlsn0w_setBackgroundColor:[UIColor clearColor]];
}




- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //设置非根控制器就设置返回按钮
    if (self.viewControllers.count > 0) {//非根控制器就创建按钮设置图片
        //设置控制器当push的时候隐藏底部的TabBar条
        viewController.hidesBottomBarWhenPushed = YES;
        //设置返回按钮
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置按钮文本
        //[backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        //监听返回按钮的点击
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        //让按钮自适应尺寸
        [backBtn sizeToFit];
        //在按钮内容有尺寸的时候,设置边缘位置
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
        //添加按钮到导航控制器上
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        
    }
    //调用super执行跳转
    [super pushViewController:viewController animated:animated];
}

//返回
- (void)back
{
    //返回上一个控制器
    [self popViewControllerAnimated:YES];
}



@end
